import { defineStore } from 'pinia'
import { webConfig, getOrderNum } from '@/api/other'
import { getMessagedNum } from '@/api/control/order'
import type { ISystemState } from './types'

const userSystemStore = defineStore('system', {
  state: (): ISystemState => ({
    usersTotalCount: 0,
    usersList: [],
    pageTotalCount: 0,
    pageList: [],
    largeLogo: '', // 大logo
    smallLogo: '', // 小logo
    publicQrcode: '', // 公众号二维码
    unreadOrder: 0, // 工单未读消息
    unreadMessage: 0, // 站内信未读消息
  }),
  actions: {
    postWebInfo(queryInfo: any) {
      webConfig(queryInfo).then((res: any) => {
        const info = JSON.parse(res.data.settingValue)
        this.largeLogo = info.largeLogo
        this.smallLogo = info.smallLogo
        this.publicQrcode = info.publicQrcode
      })
    },
    // 获取工单未读消息
    orderUnread() {
      return new Promise((resolve) => {
        getOrderNum({}).then((res: any) => {
          this.unreadOrder = res.data
          resolve(res.data)
        }).catch(() => {
          this.unreadOrder = 0
          resolve(0)
        })
      })
    },
    // 获取站内信未读消息
    messageUnread() {
      return new Promise((resolve) => {
        getMessagedNum().then((res: any) => {
          this.unreadMessage = res.data
          resolve(res.data)
        }).catch(() => {
          this.unreadMessage = 0
          resolve(0)
        })
      }) 
    }
  }
})

export default userSystemStore
